package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.WareHouseLocationEntity;
import huzofw.service.impl.GenericManagerImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.repository.master.WareHouseLocationRepository;
import cn.com.huzo.service.master.WareHouseLocationManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import huzofw.model.entities.paginator.Paginator;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;

/**
 * 仓库库位管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("WareHouseLocationManager")
public class WareHouseLocationManagerImpl extends GenericManagerImpl<WareHouseLocationEntity, String>
        implements WareHouseLocationManager {

    @Resource(name = "WareHouseLocationRepository")
    private WareHouseLocationRepository _dao_lines;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchForPage(Map<String, Object> criterion) throws Exception {
        Map<String, Object> rtnValue = new HashMap();

        Long offset = (long) (MapUtils.getInteger(criterion, "Limit") * (MapUtils.getInteger(criterion, "PageIndex") - 1));
        long total = _dao_lines.fetchForCount(criterion);

        if (total > 0) {
            if (offset >= total) {
                offset = (total - MapUtils.getInteger(criterion, "Limit")) < 0 ? 0 : (total - MapUtils.getInteger(criterion, "Limit"));
            }
            criterion.put("Offset", offset);
            List<WareHouseLocationEntity> itemList = _dao_lines.fetchForPage(criterion);
            Paginator<WareHouseLocationEntity> paginator = new Paginator<>(itemList, offset, MapUtils.getInteger(criterion, "Limit"), total);

            rtnValue.put("TotalRecordCount", paginator.getTotal());
            rtnValue.put("TotalPages", paginator.getTotalPages());
            rtnValue.put("PageIndex", paginator.getPageIndex());
            rtnValue.put("DataList", paginator.getItemList());
            rtnValue.put("Success", true);
        } else {
            rtnValue.put("Success", false);
        }

        return rtnValue;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchInsert(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        ObjectMapper mapper = new ObjectMapper();
        List<WareHouseLocationEntity> insertList = mapper.convertValue(MapUtils.getObject(criterion, "InsertList"), new TypeReference<List<WareHouseLocationEntity>>() {
        });

        Integer rowsAffected = _dao_lines.batchInsert(insertList);
        if (rowsAffected > 0) {
            // 审计日志
            //for (int i = 0; i < insertList.size(); i++) {
            //    WareHouseLocationEntity instance = insertList.get(i);
            //    writeAuditLog(insertList.get(0).getUpdatedBy(), "基础信息→仓库库位", "库位编号：" + instance.getLocationNo(), "增加");
            //}
            Map<String, Object> params = new HashMap();
            params.put("WareHouseId", MapUtils.getLong(criterion, "WareHouseId"));
            params.put("Limit", MapUtils.getInteger(criterion, "Limit"));
            params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex"));
            _result = this.fetchForPage(params);
        }

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchDelete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();

        ObjectMapper mapper = new ObjectMapper();
        List<WareHouseLocationEntity> deleteList = mapper.convertValue(MapUtils.getObject(criterion, "DeleteList"), new TypeReference<List<WareHouseLocationEntity>>() {
        });

        Integer rowsAffected = _dao_lines.batchDelete(deleteList);
        // 审计日志
        //for (int i = 0; i < deleteList.size(); i++) {
        //    WareHouseLocationEntity instance = deleteList.get(i);
        //    writeAuditLog(deleteList.get(0).getDeletedBy(), "基础信息→仓库库位", "库位编号：" + instance.getLocationNo(), "批量删除");
        //}
        if (rowsAffected > 0) {
            Map<String, Object> params = new HashMap();
            params.put("WareHouseId", MapUtils.getLong(criterion, "WareHouseId"));
            params.put("Limit", MapUtils.getInteger(criterion, "Limit"));
            params.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex"));
            _result = this.fetchForPage(params);
        }
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> deleteLines(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        // 删除
        Integer rowsAffected = _dao_lines.delete(MapUtils.getString(criterion, "LocationId"));
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
                    "基础信息→仓库库位", "库位编号（" + MapUtils.getString(criterion, "LocationNo") + "）",
                    "删除");
            Map<String, Object> itemList = new HashMap();
            itemList.put("WareHouseId", MapUtils.getLong(criterion, "WareHouseId"));
            itemList.put("Limit", MapUtils.getInteger(criterion, "Limit"));
            itemList.put("PageIndex", MapUtils.getInteger(criterion, "PageIndex"));
            _result = this.fetchForPage(itemList);
        }

        return _result;
    }
}
